#include<iostream>
#include<vector>


using namespace std;

class Solution {
public:
    void quicksortfindsmallestk(vector<int>& arr, int l, int r, int k)
    {
        if (l >= r)return;
        int key = GetRandom(arr, l, r);
        int i = l; int left = l - 1; int right = r + 1;
        while (i < right)
        {
            if (arr[i] < key)swap(arr[++left], arr[i++]);
            else if (arr[i] == key)i++;
            else swap(arr[i], arr[--right]);
        }
        //[l,left],[left+1,right-1],[right,r];
        if (left - l + 1 > k)
        {
            quicksortfindsmallestk(arr, l, left, k);
        }
        else if (right - l >= k)return;
        else
        {
            quicksortfindsmallestk(arr, right, r, k - (right - l));
        }
    }
    int GetRandom(vector<int>& arr, int left, int right)
    {
        int r = rand();
        return arr[r % (right - left + 1) + left];
    }
    vector<int> smallestK(vector<int>& arr, int k) {
        srand(time(NULL));
        quicksortfindsmallestk(arr, 0, arr.size() - 1, k);
        return { arr.begin(),arr.begin() + k };
    }
};


int main()
{
    vector<int>v = { 1,2,3 };
    int k = 0;
    vector<int>result=Solution().smallestK(v, k);
    for (auto e : result)
    {
        cout << e << " ";
    }
	return 0;
}